Imports System.Text.RegularExpressions
Public Class FormUserAED
    Public PasswordMashText As String = "1234567890qwertyabcdefJ"

    Private Sub FormUserEditor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        Me.dgv.ReadOnly = False
        Me.dgv.Columns("Active1").ReadOnly = False
        Me.dgv.Columns("RoleName1").ReadOnly = True

    End Sub
    Public Sub LoadRoleByUser(ByVal UserId As Int16)
        CmdPro.Parameters("@UserId") = UserId
        Me.dgv.DataSource = CmdPro.ExecuteDataTable("LoadRoleByUser")
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

        If Me.txtUserName.Text.Trim() = "" Then
            MsgBox("Please input user name!")
            Me.txtEmail.Focus()
            Exit Sub
        End If

        If Validator.IsEmail(Me.txtEmail.Text) = False Then
            MsgBox("Please input correct email!")
            Me.txtEmail.Focus()
            Exit Sub
        End If

        If Validator.IsStrongPassword(Me.txtPassword.Text) Then
            MessageBox.Show("Password must be strong!")
            txtPassword.Focus()
            Exit Sub
        End If

        Dim password = Crypto.PasswordHash(Me.txtUserName.Text, Me.txtPassword.Text)

        CmdPro.Parameters("@UserId") = Me.txtUserId.Text
        CmdPro.Parameters("@UserName") = Me.txtUserName.Text
        CmdPro.Parameters("@FullName") = Me.txtFullName.Text
        CmdPro.Parameters("@Email") = Me.txtEmail.Text
        CmdPro.Parameters("@Password") = password
        CmdPro.Parameters("@Locked") = Me.chkLocked.Checked
        CmdPro.Parameters("@ChangePasswordAtLogOn") = Me.chkChangePasswordAtLogon.Checked

        If Me.txtUserId.Text = "" Then
            CmdPro.ExecuteNonQueryNoParameter("AddNewUser")
            Me.txtUserId.Text = CmdPro.ExecuteScalar("QueryMaxUerId")
        Else
            ' not update password
            CmdPro.ExecuteNonQueryNoParameter("NotUpdatePassword")
            If Me.txtPassword.Text <> Me.PasswordMashText Then
                CmdPro.Parameters("@Password") = password
                CmdPro.Parameters("@UserId") = Me.txtUserId.Text
                CmdPro.ExecuteNonQueryNoParameter("UpdatePassword")
            End If
        End If


        'save role
        Try



            For Each row As DataGridViewRow In Me.dgv.Rows
                CmdPro.Parameters("@UserId") = Me.txtUserId.Text
                CmdPro.Parameters("@RoleId") = row.Cells("RoleId1").Value
                CmdPro.Parameters("@Active") = row.Cells("Active1").Value

                CmdPro.ExecuteNonQueryNoParameter("SaveRole")

            Next

            Me.DialogResult = Windows.Forms.DialogResult.OK

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

        Me.DialogResult = Windows.Forms.DialogResult.Cancel

    End Sub

    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
        For Each row As DataGridViewRow In dgv.Rows
            row.Cells(0).Value = CheckBox1.Checked

        Next
    End Sub


End Class